<?
        if($errorMsg) echo "<center>$errorMsg<center>";
        $errorMsg = "";

        if(!$details[start]) $details[start] = date("Y-m-d");
        if(!$details[end]) $details[end] = date("Y-m-d");

        $date = explode("-",$details[start]);
        $syear = $date[0];
        $smonth = $date[1];
        $sday = $date[2];

        $date = explode("-",$details[end]);
        $eyear = $date[0];
        $emonth = $date[1];
        $eday = $date[2];

         $params[action]="referer_details";
         $params[sid]=$sid;
         $url="$baseURL/$webDIR/?".encode_params($params);

?>
<table width="500" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><font class="text"><b>View Referer Reports</b></font></td>
  </tr>
</table>
<br> Select period:
<TABLE border=0 class='bTable' cellspacing='0'>
<form action='<? echo $url; ?>' method='POST'>
        <tr>
                <td>Referer:</td><td colspan=3><input type='text' name='referer' value='<?=$referer?>'></td>
        </tr>
        <tr>
                <th>Type</th>
                <th>Day</th>
                <th>Month</th>
                <th>Year</th>
        </tr>
        <tr>
                <td>Start report date</td>
                <td>
                        <select name='sday'>
                        <?
                                $opt = "<option value=''></option>";
                                for($i=1;$i<=31;$i++){
                                        if($i<10) $opt .= "<option
value='0$i'>$i</option>";
                                        else $opt .= "<option
value='$i'>$i</option>";
                                }
                                $newopt = str_replace("'$sday'","$sday
selected",$opt);
                                echo $newopt;
                        ?>
                        </select>
                </td>
                <td>
                        <select name='smonth'>
                        <?
                                $opt = "<option value=''></option>";
                                for($i=1;$i<=12;$i++){
                                        $m = date ("F",
mktime(0,0,0,$i,2,2000));
                                                if($i<10) $opt .= "<option
value='0$i'>$m</option>";
                                                else $opt .= "<option
value='$i'>$m</option>";
                                }
                                $newopt = str_replace("'$smonth'","$smonth
selected",$opt);
                                echo $newopt;
                        ?>
                        </select>
                </td>
                <td>
                        <select name='syear'>
                        <?
                                $opt = "<option value=''></option>";
                                for($i=2002;$i<=date("Y");$i++){
                                        if($i<10) $opt .= "<option
value='0$i'>$i</option>";
                                        else $opt .= "<option
value='$i'>$i</option>";

                                }
                                $newopt = str_replace("'$syear'","$syear
selected",$opt);
                                echo $newopt;
                        ?>
                        </select>
                </td>
        </tr>
        <tr>
                <td>End report date</td>

                <td>
                        <select name='eday'>
                        <?
                                $opt = "<option value=''></option>";
                                for($i=1;$i<=31;$i++){
                                        if($i<10) $opt .= "<option
value='0$i'>$i</option>";
                                        else $opt .= "<option
value='$i'>$i</option>";
                                }
                                $newopt = str_replace("'$eday'","$eday
selected",$opt);
                                echo $newopt;
                        ?>
                        </select>
                </td>
                <td>
                        <select name='emonth'>
                        <?
                                $opt = "<option value=''></option>";
                                for($i=1;$i<=12;$i++){
                                        $m = date ("F",
mktime(0,0,0,$i,2,2000));
                                                if($i<10) $opt .= "<option
value='0$i'>$m</option>";
                                                else $opt .= "<option
value='$i'>$m</option>";
                                }
                                $newopt = str_replace("'$emonth'","$emonth
selected",$opt);
                                echo $newopt;
                        ?>
                        </select>
                </td>
                <td>
                        <select name='eyear'>
                        <?
                                $opt = "<option value=''></option>";
                                for($i=2003;$i<=date("Y");$i++){
                                        if($i<10) $opt .= "<option
value='0$i'>$i</option>";
                                        else $opt .= "<option
value='$i'>$i</option>";

                                }
                                $newopt = str_replace("'$eyear'","$eyear
selected",$opt);
                                echo $newopt;
                        ?>
                        </select>
                </td>

        </tr>
        <tr>
                <td colspan='5' align='center'>
                        <input type='submit' value='View report for this
period'>
                </td>
        </tr>
</form>
</TABLE>
<?
if($details[start] && $details[end] && $referer){

        $SQL="SELECT type,count(type) as count FROM referers WHERE
referer='$referer' and date>='$details[start]' and date<='$details[end]'
GROUP BY type;";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return
0;
        while($row=mysql_fetch_array($result)){
			if($row[type]=='impression') $data[impression] =
$row[count];
			if($row[type]=='signup') $data[signup] =
$row[count];
        }

        $SQL="SELECT r.referer,s.id as id,sum(s.price) as
sp,sum(s.real_price) as srp from referers r ,sales s where r.id = s.id and
r.referer='$referer' and r.type='signup' and  r.date>='$details[start]' and
r.date<='$details[end]' group by r.id,s.order_number";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return
0;
        while($row=mysql_fetch_assoc($result)){
        	$data[price] += $row[sp];
        	$data[real_price] += $row[srp];
        	$data[transactions] += 1;
        	$resellers[$row[id]] = 1;
        }
        $data[active_resellers] = count($resellers);
}

if(!$data[impression]) $data[impression]="0";
if(!$data[signup]) $data[signup]="0";
if(!$data[price]) $data[price]="0";
if(!$data[real_price]) $data[real_price]="0";
if(!$data[transactions]) $data[transactions]="0";
if(!$data[active_resellers]) $data[active_resellers]="0";

?>
<br>
<TABLE border=0 class='bTable' cellspacing='0' cellpadding="0">
	<tr><td>Referer : </td><td>&nbsp;<?=$referer; ?></td></tr>
	<tr><td>Imprestions : </td><td>&nbsp;<?=$data[impression];
?></td></tr>
	<tr><td>Signups : </td><td>&nbsp;<?=$data[signup]; ?></td></tr>
	<tr><td>Active resellers :
</td><td>&nbsp;<?=$data[active_resellers]; ?></td></tr>
	<tr><td>Transactions : </td><td>&nbsp;<?=$data[transactions];
?></td></tr>
	<tr><td>Total transactions : </td><td>&nbsp;$&nbsp;<?=$data[price];
?></td></tr>
	<tr><td>Our profit : </td><td>&nbsp;$&nbsp;<?=$data[real_price];
?></td></tr>
	<tr><td>Resellers profit :
</td><td>&nbsp;$&nbsp;<?=($data[price]-$data[real_price]);?></td></tr>
</TABLE>
